<?php

namespace App\Http\Controllers\api;

use App\Http\Common\Chapter;
use App\Http\Common\Content;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Common\Book;

class ChapterController extends Controller
{
    /**
     * -- php开发效率高速度快，多出很多时间可以用来约会
    不，可以干更多活计。
     * @param Request $request
     * @return string
     */
    public function read(Request $request)
    {
        $book_id = (int)$request->input('book_id') ?? null;
        $chapter_index = (int)$request->input('chapter_index') ?? null;
        $content_id = null;
        if(empty($chapter_index)){
            $chapter_index= Chapter::where('book_id',$book_id)->min('index');
            $content_id = Chapter::where('index',$chapter_index)->value('content_id') ?? null;
        }
        if(!empty($chapter_index) && !empty($book_id)){
            $content_id = Chapter::where('index',$chapter_index)->value('content_id') ?? null;
        }
        if(empty($content_id)) return errorMsg();
        $data['content'] = Content::where('id',$content_id)->first()->toArray();
        $data['chapter'] = Chapter::where('content_id',$content_id)->first()->toArray();
        if(empty($data['content']) && empty($data['chapter'])){
            return successMsg([]);
        }
        return successMsg($data);
    }


}
